import http from './@/utils/request/http.js'
export default {
	//  小程序登录
	loginCommenHandle(parms){
		let that = this;
		let data ={};
		let accessToken =  uni.getStorageSync('accessToken');
		let mineInfo =  uni.getStorageSync('mineInfo');
		// 判断登陆信息
		return new Promise((resolve, reject) => {
			uni.login({
				success: function(lRes) {
					if(parms.id || parms.parentId){
						if(parms.id){
							data = {
								code: lRes.code,
								parentUid:parms.id
							}
						}
						if(parms.parentId) {
							data = {
								code: lRes.code,
								parentUid:parms.parentId,
								isInner:parms.isInner=='true'?true:false
							}
						}
					}else {
						data = {
							code: lRes.code
						}
					}
					http.post('/api/user/v1/wx/minilogin',data).then(sres => {
						switch(sres.code){
							case 200:
								uni.setStorageSync('accessToken',sres.data.accessToken);
									//  判断是否有用户信息头像昵称
								that.getUserInfo().then(Res=>{
									uni.setStorageSync('mineInfo',Res);
									resolve(Res)
								})
							break;
							default:
								uni.showToast({
									title:sres.message,
									icon:'none'
								})
							break;
						}
					})
				}
			});
		})
	},
	
	/* 
		获取用户信息
	 */
	//  用户信息
	getUserInfo() {
		return new Promise((resolve, reject) => {
			http.post('/api/v1/user/userinfo').then(lRes => {
				switch(lRes.code){
					case 200:
						uni.setStorageSync('mineInfo',lRes.data);
						resolve(lRes.data)
					break;
					case 401:
						//需要重新登录
						this.loginCommenHandle();
					break;
					default:
						uni.showToast({
							title:lRes.message,
							icon:'none'
						})
					break;
				}
			})
		})
	},
};